$(function(){
	//加载数据
	loadDatas();

});

//加载玩家在线列表数据
function loadDatas(){
	$.ajax({
		type: "post",
		url: "../../php/player/controlByIP/loadControlByIPListData.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			
			//表格数据
			var datas = msg.records;
			var html = "";
			if(datas.length == 0){
				html += "<tr><td colspan='6' align='center'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i < datas.length; i++){
					html += "<tr>";
					html += "<td>"+datas[i].ip+"</td>";//IP
					html += "<td>"+datas[i].limitScore+"</td>";//限制分数
					var type = datas[i].handCheat;
					if(type == "1"){
						type = "手动控制";
					}else if(type == "0"){
						type = "自动控制";
					}
					html += "<td>"+type+"</td>";//控制类型
					html += "<td>"+datas[i].cheatRate+"</td>";//作弊率
					html += "<td>"+datas[i].date+"</td>";//时间
					html += "<td style='font-size:16px;'>";
					html += "<span class='fa fa-pencil-square-o' style='cursor: pointer; color:#daa40b;' title='修改' onclick='update("+datas[i].rid+")'></span>";
					html += "<span class='fa fa-trash' style='margin-left:10px; cursor: pointer; color:#dd5044' title='删除' onclick='deleteInfo("+datas[i].rid+")'></span>";
					html += "</td>";
					html += "</tr>";
				}
			}
			$("#databody").html(html);
		}
	});
}

//打开编辑
function update(id){
	$.ajax({
		type: "post",
		data: {"id": id},
		url: "../../php/player/controlByIP/getControlByIPDataById.php",
		success: function(msg){
			var datas = msg.records;
			
			$("#id").val(datas[0].rid);
			$("#ip").val(datas[0].ip);
			var limitScore = datas[0].limitScore;
			$("#limitScore").val(limitScore);
			var cheatRate = datas[0].cheatRate;
			$("#cheatRate").val(cheatRate);
			$("#editWindow").modal("show");
		}
	});
}

//提交编辑
function subEdit(){
	var id = $("#id").val();
	var ip = $.trim($("#ip").val());//IP
	var limitScore = $.trim($("#limitScore").val());//限制分数
	var cheatRate = $.trim($("#cheatRate").val());//作弊率
	
	if(ip == ""){
		$("#ip").css("border","1px solid #f00");
		$("#ip_error").css("display","block");
	}else{
		$("#ip").css("border","");
		$("#ip_error").css("display","none");
	}
	if(limitScore == "" || isNaN(limitScore)){
		$("#limitScore").css("border","1px solid #f00");
		$("#limitScore_error").css("display","block");
	}else{
		$("#limitScore").css("border","");
		$("#limitScore_error").css("display","none");
	}
	if(cheatRate == "" || isNaN(cheatRate)){
		$("#cheatRate").css("border","1px solid #f00");
		$("#cheatRate_error").css("display","block");
	}else{
		$("#cheatRate").css("border","");
		$("#cheatRate_error").css("display","none");
	}
	
	if(ip != "" && limitScore != "" && !isNaN(limitScore) && cheatRate != "" && !isNaN(cheatRate)){
//		alert("id: "+id+"; ip: "+ip+"; limit: "+limitScore+"; cheat: "+cheatRate);
		$.ajax({
			type: "post",
			data: {"id":id, "ip":ip, "limitScore":limitScore, "cheatRate":cheatRate},
			url: "../../php/player/controlByIP/editControlByIP.php",
			beforeSend: function(){
				$("#load").css("display","block");
			},
			success: function(msg){
				$("#load").css("display","none");
				if(msg == "ok"){
					cannelEdit();
					$("#tishi").modal("show");
				}else if(msg == "error"){
					alert("出错啦！");
				}else{
					alert(msg);
				}
			}
		});
	}
	
}

//取消编辑
function cannelEdit(){
	$("#resetForm").click();
	$("#ip, #limitScore, #cheatRate").css("border","");
	$("#ip_error, #limitScore_error, #cheatRate_error").css("display","none");
	$("#editWindow").modal("hide");
}

//打开删除确认框
function deleteInfo(id){
	$("#delId").val(id);
	$("#confirmWindow").modal("show");
}

//提交删除
function subDel(){
	var id = $("#delId").val();
	$.ajax({
		type: "post",
		data: {"id":id},
		url: "../../php/player/controlByIP/deleteControlByIP.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		success: function(msg){
			$("#load").css("display","none");
			if(msg == "ok"){
				cannelDel();
				$("#tishi").modal("show");
			}else if(msg == "error"){
				alert("出错啦！");
			}else{
				alert(msg);
			}
		}
	});
}

//取消删除
function cannelDel(){
	$("#delId").val("");
	$("#confirmWindow").modal("hide");
}
